<?php

namespace App\Http\Controllers\Console\QingShanERP;

use App\Http\Controllers\Controller;
use App\Models\QingShanERP\Logistics;
use Illuminate\Http\Request;
use App\Customize\PddRequest;
use DB;
use Validator;

class LogisticsController extends Controller
{
    public function getList(Request $req)
    {
        try {
            $page = $req->input('page', 0);
            $size = $req->input('size', 10);
            $type = $req->input('type', 0);
            $key = $req->input('key', '');
            $list = Logistics::where(function ($q) use ($type) {
                if ($type != 0) {
                    $q->where('type', $type);
                }
            })->where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('logistics_company', 'like', '%' . $key . '%');
                }
            })->where('del_flag', 0)->offset(($page - 1) * $size)->limit($size)->orderBy('id', 'desc')->get();
            $count = Logistics::where(function ($q) use ($type) {
                if ($type != 0) {
                    $q->where('type', $type);
                }
            })->where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('logistics_company', 'like', '%' . $key . '%');
                }
            })->where('del_flag', 0)->count();
            $res = ['list' => $list, 'total' => $count];
            return $this->resData(200, '获取数据成功...', $res);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请稍后再试...');
        }
    }
    // 同步拼多多物流信息，出来的太多了，其实没有用
    public function postSyncPdd(){
        DB::beginTransaction();
        try {
            $result = PddRequest::request('pdd.logistics.companies.get');
            if (isset($result['logistics_companies_get_response'])) {
                $insert = [];
                $date = date('Y-m-d H:i:s');
                foreach ($result['logistics_companies_get_response']['logistics_companies'] as $v) {
                    if ($v['available']) {
                        $insert[] = ['type'=>4, 'logistics_company'=>$v['logistics_company'], 'logistics_id' => $v['id'], 'logistics_code' => $v['code'],'last_at'=>$date, 'created_at' =>$date, 'updated_at' => $date];
                    }
                }
                DB::commit();
                return $this->resData(200, '同步数据成功...', $insert);
            }
            else {
                DB::rollback();
                return $this->resData(400, '请求快递公司信息失败...'. $result['error_response']['error_msg']);
            }
        } catch (\Throwable $e) {
            DB::rollback();
            return $this->resData(500, '获取数据失败，请稍后再试...'.$e->getMessage());
        }
    }


    public function postCreate(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'type' => 'required|in:1,2,3,4',
                'logistics_company' => 'required|max:20',
                'logistics_id' => 'required|max:100',
                'logistics_code' => 'required|max:100',
            ]);
            $attrs = array(
                'type' => '类型',
                'logistics_company' => '公司名称',
                'logistics_id' => '公司编码',
                'logistics_code' => '公司代码'
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $all = $request->all();
            $create = ['type' => $all['type'], 'logistics_company' => $all['logistics_company'], 'logistics_id' => $all['logistics_id'], 'logistics_code' => $all['logistics_code']];
            Logistics::create($create);
            return $this->resData(200, '添加成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '添加文章失败，请重新操作...');
        }
    }
    //详情
    public function postDetail(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'logistics_id' => 'required|integer',
            ]);
            $attrs = array(
                'logistics_id' => '公司ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('logistics_id');
            $info = Logistics::find($id);
            return $this->resData(200, '获取数据成功...', $info);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请重新操作...');
        }
    }
    //编辑
    public function postEdit(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'l_id' => 'required|integer',
                'type' => 'required|in:1,2,3,4',
                'logistics_company' => 'required|max:20',
                'logistics_id' => 'required|max:100',
                'logistics_code' => 'required|max:100',
            ]);
            $attrs = array(
                'l_id' => '公司ID',
                'type' => '类型',
                'logistics_company' => '公司名称',
                'logistics_id' => '公司编码',
                'logistics_code' => '公司代码'
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('l_id');
            $all = $request->all();
            $update = ['type' => $all['type'], 'logistics_company' => $all['logistics_company'], 'logistics_id' => $all['logistics_id'], 'logistics_code' => $all['logistics_code']];
            Logistics::where('id', $id)->update($update);
            return $this->resData(200, '编辑成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '编辑失败，请重新操作...');
        }
    }

    //删除
    public function postRemove(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'logistics_id' => 'required|array',
            ]);
            $attrs = array(
                'logistics_id' => '公司ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('logistics_id');
            Logistics::whereIn('id', $id)->update(['del_flag' => 1]);
            return $this->resData(200, '删除成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '删除失败，请重新操作...');
        }
    }
}
